package org.itboys.core.service.mongo.front;

import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.village.SundayVillageDto;
import org.itboys.core.entity.mongo.SundayVillage;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/8
 * @Description：移动端-小区service
 */
@Service
public class SundayFrontVillageService extends BaseMongoService<SundayVillage> {
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayVillage> getEntityClass() {
        return SundayVillage.class;
    }

    /**
     * 分页查询小区
     * 精确查询传入参数省市区id
     * 模糊查询小区名称
     * @param request
     * @return
     */
    public List<SundayVillageDto> select(HttpServletRequest request){
        List<SundayVillageDto> result=new ArrayList<>();
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request, "provinceId", "cityId", "districtId");
        param.put("isDeleted", SundayCommonConstants.NO);
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name");
        PageResult<SundayVillage> pageResult = super.containpageQuery(request, vagueParam, param);
        List<SundayVillage> data = pageResult.getData();
        return packageDtos(data);
    }

    public  List<SundayVillageDto> packageDtos(List<SundayVillage> villages){
        List<SundayVillageDto> result=new ArrayList<>();
        for (SundayVillage village:villages
             ) {
            SundayVillageDto dto=new SundayVillageDto();
            dto.setId(village.getId());
            dto.setName(village.getName());
            dto.setAddress(village.getProvinceName()+village.getCityName()+village.getDistrictName());
            result.add(dto);
        }
        return result;
    }
}
